package com.tfleo.mealssenger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import com.github.ignition.core.widgets.RemoteImageView;
import com.tfleo.mealssenger.R;
import com.tfleo.mealssenger.adapter.FriendCheckBoxAdapter;
import com.tfleo.mealssenger.adapter.GuestCheckBoxAdapter;
import com.tfleo.mealssenger.adapter.HangoutListAdapter;
import com.tfleo.mealssenger.object.FriendSelection;
import com.tfleo.mealssenger.object.Guest;
import com.tfleo.mealssenger.object.Hangout;
import com.tfleo.mealssenger.utility.DataFetcher;
import com.tfleo.mealssenger.utility.StaticMapFetcher;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.Settings.Secure;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class HangoutActivity extends Activity {
	private LayoutInflater mInflater;
	private ListView vGuestList;
	private TextView vHostName;
	private TextView vPlaceName;
	private TextView vPlaceAddress;
	private TextView vDate;
	private TextView vMessage;
	private RemoteImageView vHostImage;
	private RemoteImageView vPlaceMap;
	
	private int mMyId;
	private int mHangoutId;
	private Hangout mHangout;
	
	private ArrayList<Guest> mGuestList;
	
	private SharedPreferences mSharedPref;

	private GuestCheckBoxAdapter mGuestAdapter;
	
	private Context mContext;
	private String mAuth;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.hangout_layout);
		mContext = this;
		mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
		mAuth = Secure.getString(this.getContentResolver(), Secure.ANDROID_ID); //for authentication
		mSharedPref = getSharedPreferences(Main.USER_DATA, MODE_PRIVATE);
		mMyId = mSharedPref.getInt(Main.USER_ID, 0);
		
		Bundle extras = getIntent().getExtras();
        if(extras != null) {
        	mHangoutId = extras.getInt("id", 0);
        } else {
        	Toast.makeText(mContext, "Error: Bundle is null!", Toast.LENGTH_SHORT);
        	finish();
        }
		
        vPlaceName = (TextView) findViewById(R.id.place_name);
        vPlaceAddress = (TextView) findViewById(R.id.place_addr);
        vPlaceMap = (RemoteImageView) findViewById(R.id.map_img);
        vDate = (TextView) findViewById(R.id.date);
        vHostName = (TextView) findViewById(R.id.host_name);
        vHostImage = (RemoteImageView) findViewById(R.id.host_img);
		vMessage = (TextView) findViewById(R.id.message);
		vGuestList = (ListView) findViewById(R.id.guest_list);
		
		FetchHangoutTask task = new FetchHangoutTask();
		task.execute(mHangoutId);
	}
	
	private class FetchHangoutTask extends AsyncTask<Integer, Void, Boolean> {
		private ProgressDialog mProgressDialog;
		@Override    
	    protected void onPreExecute() {       
	        super.onPreExecute();
	        mProgressDialog = new ProgressDialog(mContext);
	        mProgressDialog.setMessage("Loading...");
	        mProgressDialog.setCancelable(false);
	        mProgressDialog.setIndeterminate(true);
	        mProgressDialog.show();
	    }
		
		@Override
		protected Boolean doInBackground(Integer... id) {
			mHangout = DataFetcher.fetchHangout(id[0]);
			if(mHangout == null) {
				return false;
			}
			mGuestList = DataFetcher.fetchGuestList(id[0]);
			if(mGuestList == null) {
				return false;
			}
			return true;
		}
		
		@Override
		protected void onPostExecute(Boolean result) {
			if(result) {
				setupView();
				mProgressDialog.dismiss();
    		} else {
    			Toast.makeText(mContext, "Error: Connection with server failed!", Toast.LENGTH_SHORT).show();
    			mProgressDialog.dismiss();
    		}
	    }
		
	}
	
	private void setupView() {
		vPlaceName.setText(mHangout.placeName);
		vPlaceAddress.setText(mHangout.placeAddress);
        vDate.setText(mHangout.date);
        vHostName.setText(mHangout.hostName);
        vHostImage.setImageUrl(mHangout.hostImageUrl);
        vHostImage.loadImage();
		vMessage.setText(mHangout.message);
		
		//vPlaceMap.setImageUrl(StaticMapFetcher.getStaticMapUrl(mHangout.placeLon, mHangout.placeLat));
		vPlaceMap.setImageUrl(StaticMapFetcher.getFromLocal(mHangout.placeMap));
		vPlaceMap.loadImage();
		
		mGuestAdapter = new GuestCheckBoxAdapter(mContext, 
        		R.layout.guest_entry, R.id.name, mGuestList, mInflater, mHangoutId, mMyId, mAuth);
        vGuestList.setAdapter(mGuestAdapter);
	}
	

  
}
